package com.itheima.upload.banben5;

import java.io.*;
import java.net.Socket;

/*
发送端
上传文件
并接收结果
 */
public class Client1 {

    public static void main(String[] args) throws IOException {
        //客户端思路:
        //创建发送端对象 cs
        for (int i=0;i<100;i++) {
            new Thread(()->{
                try {

                    System.out.println();
                    Socket cs = new Socket("127.0.0.1", 18888);
                    //从发送端对象中获取输出流(字节)cs.getOutputStream
                    OutputStream os = cs.getOutputStream();
                    //缓冲字节输出流  包装字节输出流  bos
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    //创建本地缓冲输入流 读文件 bis
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day16\\a.png"));
                    //使用缓冲流写数据
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len=bis.read(bytes))!=-1){
                        bos.write(bytes,0,len);
                    }

                    //刷新缓存 bos.flush
                    bos.flush();
                    //暂停输入cs.shutdownoutput
                    cs.shutdownOutput();
                    //从发送端对象中获取输入流(字节)cs.getInputStream
                    InputStream is = cs.getInputStream();
                    //将字节输入流转换成字符输入流(InputStreamReader) reader
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    //读数据  reader.readLine()
                    String result;
                    while ((result=reader.readLine())!=null){
                        System.out.println(result);
                    }
                    //释放资源)
                    reader.close();
                    bis.close();
                    bos.close();
                    cs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }).start();
        }
    }
}
